<template>

  <div>
    <div class="filter-container">
      <div class="filter-item">
        <span>名称:</span>
        <el-input v-model="listQuery.name" clearable placeholder="请输入名称" @keyup.enter="handleFilter" />
      </div>
      <el-button v-permission="['sys:organization:list']" class="filter-item" type="primary" icon="Search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" text type="primary" icon="Delete" @click="clearQuery">清除条件</el-button>
    </div>
    <div class="operationList">
      <div>
        <el-button v-permission="['sys:organization:add']" text type="primary" icon="Plus" @click="handleCreate">新增</el-button>
      </div>
      <div>
        <el-button text type="primary" icon="RefreshRight" @click="handleFilter">刷新</el-button>
      </div>
    </div>
    <el-table v-loading="listLoading" :row-class-name="tableRowClass" :data="list" row-key="id" header-cell-class-name="header-cell">
      <el-table-column prop="name" label="名称" width="180" />
      <el-table-column label="备注">
        <template #default="scope">
          <span>{{ scope.row.remarks }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button v-permission="['sys:organization:update']" size="small" type="primary" plain @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-permission="['sys:organization:delete']" size="small" plain type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <organization-form ref="form" @refresh-list="refreshList" />

  </div>
</template>

<script>
import { fetchOrganizationList, deleteOrganization } from '@/api/sys/organization/organization'
import OrganizationForm from './organizationForm'
import { treeMixin } from '@/mixin'
export default {
  name: 'OrganizationList',
  components: { OrganizationForm },
  mixins: [treeMixin],
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        name: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /**
     * 刷新列表
     */
    getList(loading = true) {
      this.listLoading = loading
      this.list = []
      fetchOrganizationList(this.listQuery).then(response => {
        if (response.data.success) {
          this.list = response.data.data
        }
        this.listLoading = false
      })
    },
    /**
     *
     * @param row 待刷新的行数据
     */
    refreshList(row) {
      const result = {}
      this.searchTree(this.list, row, result)
      this.refreshTree(this.list, row, result)
    },
    /**
     * 搜索
     */
    handleFilter() {
      this.getList()
    },
    /**
     * 新增
     */
    handleCreate() {
      this.$refs.form.setList(this.list)
      this.$refs.form.handleCreate()
    },
    /**
     * 编辑
     * @param row 待编辑的数据
     */
    handleUpdate(row) {
      this.$refs.form.setList(this.list)
      this.$refs.form.handleUpdate(row.id)
    },
    /**
     * 删除
     * @param row 待删除的数据
     */
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteOrganization([row.id]).then(response => {
          if (response.data.success) {
            this.deleteTree(this.list, row)
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    }

  }
}
</script>
